package com.leyou.putGoods.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.mall.data.entity.Result;
import com.leyou.putGoods.entity.PutGoods;
import com.leyou.putGoods.entity.PutGoodsVo;
import com.leyou.putGoods.service.IPutGoodsService;
import com.leyou.shop.entity.UpdateLog;
import com.leyou.wareHouse.entity.MallGoods;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cyb
 * @since 2022-07-25
 */
@RestController
@RequestMapping("/put-goods")
public class PutGoodsController {

    @Resource
    private IPutGoodsService putGoodsService;

    /**
     * @description: 新增和修改
     * @author 车矣捕
     */
    @PostMapping("/save")
//    public Result save(@RequestParam(value = "mallGoods",required=true) List<MallGoods> mallGoods) {
    public Result save(@RequestBody List<PutGoods> putGoods) {

//        putGoods
//        System.err.println(putGoods.toString());
//        System.err.println(putGoods.size());
//        System.err.println(mallGoods.size());
//        System.err.println(mallGoods.toString());
        if (putGoods.size() == 0) {
            return Result.error(400,"sb");
//        }else if (putGoods.size() == 1) {
//            int i = putGoodsService.savePutGood(putGoods);
        }else {
            int i = putGoodsService.savePutGoods(putGoods);
        }

//        putGoodsService.savePutGoods(null);

        return Result.success();
        }

    /**
     * @description: 删除
     * @author 车矣捕
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        putGoodsService.removeById(id);
        return Result.success();
        }

    /**
     * @description: 批量删除
     * @author 车矣捕
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        putGoodsService.removeByIds(ids);
        return Result.success();
        }

    /**
     * @description: 查询所有
     * @author 车矣捕
     */
    @GetMapping
    public Result findAll() {
        return Result.success(putGoodsService.list());
    }

    /**
     * @description: 根据id查询
     * @author 车矣捕
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(putGoodsService.getById(id));
    }

    /**
     * @description:  分页查询
     * @author 车矣捕
     */
    @PostMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam(defaultValue = "") String endTime,
                           @RequestParam(defaultValue = "") String startTime,
                           @RequestParam(defaultValue = "") String statu,
                            @RequestParam Integer pageSize) {
        /*QueryWrapper<PutGoods> queryWrapper = new QueryWrapper<>();
        if (!"".equals(statu)){
            queryWrapper.like("statu",statu);
        }
        if (!"".equals(startTime)){
            queryWrapper.ge("create_time",startTime);
        }
        if (!"".equals(endTime)){
            queryWrapper.le("create_time",endTime);
        }
        queryWrapper.orderByDesc("id");
        return Result.success(putGoodsService.page(new Page<>(pageNum, pageSize), queryWrapper));*/
        return Result.success(putGoodsService.findTree( endTime, startTime,statu));
        }

    @PostMapping("updateStatu")
    public Result updateStatu(UpdateLog updateLog) {
        System.err.println(updateLog.toString());
        putGoodsService.updateStatu(updateLog);

        return Result.success();
    }

}

